﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kardex_Entity;

namespace Kardex_DataAccess
{
    public class SupervisorDA
    {
           bd_kardexEntities context;
           public SupervisorDA()
         {
             context = new bd_kardexEntities();
         }

           public List<person> GetListSupervisor()
        {
            List<person> ListSupervisor = null;
            try
            {
                var q = from s in context.supervisor
                        from p in context.person
                        where p.IdPerson == s.IdPerson
                        orderby p.IdPerson
                        select p;
                ListSupervisor = q.ToList();

                return ListSupervisor;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
           public person GetObjByIdperson( int IdPerson)
           {
               try
               {
                   var q = from s in context.supervisor
                        from p in context.person
                        where p.IdPerson == s.IdPerson &&
                        s.IdPerson==IdPerson
                        orderby p.IdPerson
                        select p;
                   person objPerson = (person) q.FirstOrDefault();
                  
                   return objPerson;
               }
               catch (Exception ex)
               {                   
                    throw new Exception(ex.Message.ToString());
               }
           }

        public person Save(person NewPerson)
        {
            try
            {
                context.person.AddObject(NewPerson);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return NewPerson;
        }

        public bool Udate(person PersonModif)
        {
            bool ReturnResult = false;
            try
            {
                context.person.Attach(PersonModif);
                context.ObjectStateManager.ChangeObjectState(PersonModif, System.Data.EntityState.Modified);
                context.SaveChanges();

                context.supervisor.Attach(PersonModif.supervisor);
                context.ObjectStateManager.ChangeObjectState(PersonModif.supervisor, System.Data.EntityState.Modified);
                context.SaveChanges();

                ReturnResult = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return ReturnResult;
        }
           
           public bool Delete(person PersonDelete)
           {
               bool ReturnResult = false;
               try
               {
                   context.person.Attach(PersonDelete);
                   context.DeleteObject(PersonDelete);
                   context.SaveChanges();
                   ReturnResult = true;
               }
               catch (Exception ex)
               {
                   throw new Exception(ex.Message.ToString());
               }
               return ReturnResult;
           }
    }
}
